package h0812;

import java.util.Arrays;
import java.util.Scanner;

/**
  * @description 高效的任务规划
  * @author 不知名帅哥
  * @date 2024/8/12 11:43
  * @version 1.0
*/
public class EfficientTaskPlanning {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int m = sc.nextInt();

        int[][][] tasks=new int[m][][];
        for (int i = 0; i < m; i++) {
            int n = sc.nextInt();
            int[][] task = new int[n][2];
            for (int j = 0; j < n; j++) {
                task[j][0]=sc.nextInt();
                task[j][1]=sc.nextInt();
            }
            tasks[i]=task;
        }
        getResult(tasks);
    }

    private static void getResult(int[][][] tasks) {
        for (int[][] task : tasks) {
            //将每个任务中的机器工作顺序按照运行时间降序排序
            Arrays.sort(task,(a,b)->b[1]-a[1]);

            int config_endTime=0;
            int ans=0;
            for (int[] info : task) {
                int config_cost = info[0];
                int run_cost = info[1];

                config_endTime+=config_cost;
                ans=Math.max(ans,config_endTime+run_cost);
            }

            System.out.println(ans);
        }
    }
}
